"""
功能描述 ：
编写人 ：
编写日期 ：
实现逻辑 ：
    1- ......
        1.1- ......
    2- ......
        2.1- ......
"""
# #异常
# try:
#     print(1/0)
# except(NameError,ZeroDivisionError):#指定异常
#     print('有错误')
#
# try:
#     print(1/0)
# except Exception as result: #所有异常
#     print(result)
#
# try:
#     print(1)
# except Exception as result:
#     print(result)
# else:
#     print('没有异常时执行的代码')
#
# try:
#     f = open('test.txt')
# except Exception as result:
#     f = open ('test.txt','w')
# else:
#     print("没有异常执行的代码")
# finally:
#     f.close()#有无异常都执行的代码

# 异常传递
# 需求：
# 1. 尝试只读⽅式打开test.txt⽂件，如果⽂件存在则读取⽂件内容，⽂件不存在则提示⽤户即可。
# 2. 读取内容要求：尝试循环读取内容，读取过程中如果检测到⽤户意外终⽌程序，则 except 捕获异常并提示⽤户
import time
# try:
#     f = open('test.txt','r')
#     try:
#         while True:
#             content = f.readline()
#             if len(content) ==0:
#                 break
#                 time.sleep(3)
#                 print(content)
#     except:
#         print("意外终止")
#     finally:
#         f.close()
# except:
#     print("文件不存在")
#
# # 自定义异常
# # 需求：密码⻓度不⾜，则报异常（⽤户输⼊密码，如果输⼊的⻓度不⾜3位，则报错，即抛出⾃定义异常，并捕获该异常）
# class ShortInputError(Exception):
#     def __init__(self,length,min_len):
#         self.length = length
#         self.min_len = min_len
#     def __str__(self):
#         return "f'你输入的长度是{self.length},不能少于{slef.min_len}个字符'"
# def main():
#     try:
#         con = input('请输入密码：')
#         if len(con) <3:
#             raise ShortInputError(len(con),3)
#     except Exception as result:
#         print(result)
#     else:
#         print("密码已输入完成")
# main()

#面向对向
# class Waser():
#     def wash(self):
#         print('我会洗衣服')
# Haier = Waser()
# Haier.wash()
#
# class Waser():
#     def wash(self,width,height):
#         self.width = width
#         self.height = height
#     def print_info(self):
#         print(f'洗衣机宽度{self.width},高度是{self.height}')
# Haier = Waser(100,200)
# Haier.print_info()

"""
烤地瓜需求：
1. 被烤的时间和对应的地⽠状态：
    0-3分钟：⽣的
    3-5分钟：半⽣不熟
    5-8分钟：熟的
    超过8分钟：烤糊了
2. 添加的调料：
⽤户可以按⾃⼰的意愿添加调料
"""
"""
需求分析：
定义类SweetPotato()
    初始化__init__，地瓜的属性
        1-被烤的时间
        2-地瓜的状态
        3-添加的调理
    地瓜的方法cook
        被烤
            1-设定每次烤地瓜的时间
            2-判断地瓜状态
        添加调料
"""
# class SweetTomato():
#     def __init__(self):
#         self.cook_time = 0
#         self.cook_static ='生的'
#         self.condiments = []
#     def cook(self,time):
#         self.cook_time += time
#         if 0 <= self.cook_time <= 3:
#             self.cook_static = '生的'
#         elif 3 <= self.cook_time <= 6:
#             self.cook_static = '半生不熟的'
#         elif 6 <= self.cook_time <= 8:
#             self.cook_static = '熟透了'
#         elif self.cook_time >=8:
#             self.cook_static = '糊了'
#     def add_condiments(self,condiments):
#         self.condiments.append(condiments)
#     def __str__(self):
#         return (f'地瓜考了{self.cook_time}分钟，状态是{self.cook_static},添加的调料有{self.condiments}')
#
# sp = SweetTomato()
# sp.cook(5)
# sp.add_condiments('辣椒面')
# print(sp)
#
# # 搬家具
# # 将⼩于房⼦剩余⾯积的家具摆放到房⼦中
# #家具类
# class Furniture():
#     def __init__(self,name,area):
#         self.name = name
#         self.aera = area
#     #房子类
# class Home():
#     def __init__(self,address,area):
#         self.address = address
#         self.aera = area
#         self.free_area = area
#         self.furniture = []
#
#     def __str__(self):
#         return f'房⼦坐落于{self.address}, 占地⾯积{self.area}, 剩余⾯积{self.free_area}, 家具有{self.furniture}'
#     def add_funiture(self,item):
#         if self.free_area >= item.aera:
#             self.free_area -= item.aera
#         else:
#             print('家具太大，剩余面积不足，无法容纳')
#
# bed = Furniture('双⼈床', 6)
# jia1 = Home('北京', 1200)
# print(jia1)
# jia1.add_furniture(bed)
# print(jia1)
# sofa = Furniture('沙发', 10)
# jia1.add_furniture(sofa)
# print(jia1)
# ball = Furniture('篮球场', 1500)
# jia1.add_furniture(ball)
# print(jia1)

# 1）需求：
# 1.士兵瑞恩有一把AK47
# 2.士兵可以开火(士兵开火扣动的是扳机)
# 3.枪能够发射子弹–把子弹发射出去
# 4.枪能够装填子弹–增加子弹的数量
class Gun:
    def __init__(self,model,bullet_count):
        self.model = model
        self.bullet_count = bullet_count
    def __str__(self):
        return "%s，它有%d颗子弹" %(self.model,self.bullet_count)
    def shoot(self):
        if self.bullet_count == 0:
            return False
        else:
            self.bullet_count -= 1
            print("正在射击...已经射中目标!")
            return True
    def add_bullet(self,count):
            self.bullet_count += count
            print("已经填充了%d颗子弹" % count)
            return True
class Soldier:
    def __init__(self,name):
        self.name = name
        self.gun = None
    def __str__(self):
        return "%s有一把%s" % (self.name,self.gun)
    def fire(self):
        if self.gun == None:
            return False
        else:
            self.gun.add_bullet(10)
            self.gun.shoot()
            return True

B = Gun("ak47",30) #将枪实例化
A = Soldier("恩德")
A.gun = B  #将实例化的枪给士兵
A.fire()
print(A)




